﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Newtonsoft.Json.Serialization;

namespace ZR.Infrastructure.Resolver;

public class JsonPropertyContractResolver : CamelCasePropertyNamesContractResolver
{
    private HashSet<string> _propertySet;
    
    public JsonPropertyContractResolver(IEnumerable<string> propertyNames)
    {
        if (propertyNames != null)
        {
            _propertySet = new HashSet<string>(propertyNames, StringComparer.Ordinal);
        }
    }

    // protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
    // {
    //     var list = base.CreateProperties(type, memberSerialization).ToList();
    //     var resList = list
    //         .Where(a => !this._lstInclude.Contains(a.PropertyName))
    //         .ToList();
    //     return resList;
    //     // .FindAll(p => _lstInclude.Contains(p.PropertyName));
    // }

    protected override List<MemberInfo> GetSerializableMembers(Type objectType)
    {
        List<MemberInfo> serializableMembers = null;
        var allMembers = base.GetSerializableMembers(objectType);

        if (_propertySet != null && _propertySet.Count > 0)
        {
            serializableMembers = allMembers.Where(m => !_propertySet.Contains(m.Name)).ToList();
        }
        return serializableMembers != null && serializableMembers.Count > 0 ? serializableMembers : allMembers;
    }
}